package com.qf.web;

import com.qf.others.MyConvert;
import com.qf.pojo.User;
import com.qf.service.UserService;
import com.qf.service.serviceimpl.UserServiceImpl;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.Map;
/**
 * 修改用户信息
 * */
@WebServlet("/modifyUserServlet")
public class ModifyUserServlet extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        //接受用户参数
        Map<String, String[]> users = request.getParameterMap();
        /*Set<String> key = users.keySet();
        for(String s : key){
            String[] value = users.get(s);
            System.out.println(key+"--"+value);
        }*/
        User user = new User();
        //日期转换器,将前台的日期String转换为Date类型
        ConvertUtils.register(new MyConvert(), Date.class);
        try {
            BeanUtils.populate(user,users);//将修改的前台参数封装到user对象中
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        //调用业务层
        UserService us = new UserServiceImpl();
        us.modifyUser(user);
        request.getRequestDispatcher("/findUserByPage").forward(request,response);
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }
}
